package com.atguigu.gmall.file;

import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import io.minio.PutObjectOptions;
import io.minio.SetBucketPolicyArgs;
import io.minio.errors.MinioException;
import org.apache.commons.io.FilenameUtils;

import java.io.FileInputStream;

public class FileUploader {
  public static void main(String[] args) throws Exception{
    try {
      // 使用MinIO服务的URL，端口，Access key和Secret key创建一个MinioClient对象
      MinioClient minioClient = new MinioClient(
              "http://39.99.159.121:9000/",
              "admin",
              "admin123456");

      String bucketName = "java2011161234";
      // 检查存储桶是否已经存在
      boolean isExist = minioClient.bucketExists(bucketName);
      if(isExist) {
        System.out.println("Bucket already exists.");
      } else {
        // 创建一个名为asiatrip的存储桶，用于存储照片的zip文件。
        minioClient.makeBucket(bucketName);

        //StringBuilder bucketPolicy = FileUploader.createBucketPolicy(bucketName);
        /*minioClient.setBucketPolicy(
                SetBucketPolicyArgs.builder().
                        bucket(bucketName).
                        config(bucketPolicy.toString()).build());*/
      }

      FileInputStream fileInputStream = new FileInputStream(
              "D:\\尚品汇商城\\资料\\图片\\oppoK5\\2ea403d0965bcdf5.jpg");

      //http://39.99.159.121:9000/java201116/e62d3fa0ccadb911.jpg
      String fileName = System.currentTimeMillis() + ".jpg";
      // 使用putObject上传一个文件到存储桶中。
/*      minioClient.putObject(bucketName,
              fileName,fileInputStream, new PutObjectOptions(
                      fileInputStream.available(),//文件大小 3KB
                      5242880L));//对文件设置允许上传的大小   最小不能设置低于5M 最大不能高于5G*/
      System.out.println("http://39.99.159.121:9000/" + bucketName + "/" + fileName);
      minioClient.putObject(PutObjectArgs.builder()
                .bucket(bucketName)
                .object(fileName)
                .stream(fileInputStream,fileInputStream.available(),-1)
                .contentType("application/octet-stream").build());
    } catch(MinioException e) {
      System.out.println("Error occurred: " + e);
    }
  }

  public static StringBuilder createBucketPolicy(String bucketName){
    StringBuilder builder = new StringBuilder();
    builder.append("{\n");
    builder.append("    \"Statement\": [\n");
    builder.append("        {\n");
    builder.append("            \"Action\": [\n");
    builder.append("                \"s3:GetBucketLocation\",\n");
    builder.append("                \"s3:ListBucket\"\n");
    builder.append("            ],\n");
    builder.append("            \"Effect\": \"Allow\",\n");
    builder.append("            \"Principal\": \"*\",\n");
    builder.append("            \"Resource\": \"arn:aws:s3:::\"+ bucketName +\"\n");
    builder.append("        },\n");
    builder.append("        {\n");
    builder.append("            \"Action\": \"s3:GetObject\",\n");
    builder.append("            \"Effect\": \"Allow\",\n");
    builder.append("            \"Principal\": \"*\",\n");
    builder.append("            \"Resource\": \"arn:aws:s3:::\"+bucketName+\"/*.**\n");
    builder.append("        }\n");
    builder.append("    ],\n");
    builder.append("    \"Version\": \"2012-10-17\"\n");
    builder.append("}\n");

    System.out.println(builder.toString());
    return builder;
  }
}